package com.webamc.dao.db4o;

import com.webamc.dao.IKeyDao;
import com.webamc.model.KeyBean;
import com.webamc.util.DB4OUtil;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * Created by Prash.
 * User: Choti
 * Date: Mar 29, 2009
 * Time: 2:59:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class KeyDao implements IKeyDao {
    private static final Logger log = Logger.getLogger(KeyDao.class);

    public KeyBean getKeyBean() {
        List<KeyBean> keyBeans = DB4OUtil.getAll(KeyBean.class);
        if (keyBeans.size() == 1) {
            return keyBeans.get(0);
        } else if (keyBeans.size() > 0) {
            this.clearKeyBeans(); //TODO - risk item
        }

        return null;
    }

    public void saveKeyBean(KeyBean k) {
        boolean t = DB4OUtil.update(k);
        log.debug("saving keyBean " + (t ? "" : "un") + "successful");
    }

    public void clearKeyBeans() {
        log.warn("Removing all instances of KeyBeans from DB");
        DB4OUtil.clear(KeyBean.class);
    }
}
